import 'package:fate_blind_box/routes/page_route_names.dart';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';

import '../theme/theme_color.dart';
import '../ui/user/Settings/complaint_list_submit_page.dart';
import '../utils/confirm_alert.dart';
import 'addgroupmember.dart';

//好友详情
class PersonDatail extends StatefulWidget {
  Map? userInfo;

  PersonDatail({this.userInfo, Key? key}) : super(key: key);

  @override
  State<PersonDatail> createState() => _PersonDatailState();
}

class _PersonDatailState extends State<PersonDatail> {
  late Map _userInfo;
  bool _switchSelected = false; //维护单选开关状态
  bool _switchSelected1 = false; //维护单选开关状态
  bool _switchSelected2 = false; //维护单选开关状态

  @override
  void initState() {
    super.initState();
    _userInfo = widget.userInfo ?? {};
     initData();
  }
  void initData() async {
   String hxId= _userInfo['easeuserId'];
    try {
      List<String> blackList =
      await EMClient.getInstance.contactManager.getBlockListFromServer();
      if(hxId.isNotEmpty&&blackList.isNotEmpty){
        if(blackList.contains(hxId)){
          setState(() {
            _switchSelected=true;
          });
        }
      }

    } on EMError catch (e) {
      debugPrint("error code: ${e.code}, desc: ${e.description}");
    }
  }
  @override
  Widget build(BuildContext context) {
    Color bgColor = const Color(0xffEDEDED);
    return AppWidget.navTitleBar(
      bgColor: bgColor,
      appBarColor: bgColor,
      title: "聊天设置",
      childWidget: Container(
        color: bgColor,
        child: Column(
          children: [
             _buildHead(),
            _buildCent(),
          ],
        ),
      ),
    );
  }

  Widget _buildCent() {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          Divider(
            indent: 0.0,
            endIndent: 0.0,
            height: 6,
            thickness: 6,
            color: Colors.grey[200],
          ),
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, left: 19, right: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "查找聊天内容",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       Icon(
          //         Icons.navigate_next,
          //         color: ThemeColor.textColorSecond,
          //         size: 20,
          //       ),
          //     ],
          //   ),
          // ),
          // Divider(
          //   indent: 0.0,
          //   endIndent: 0.0,
          //   height: 6,
          //   thickness: 6,
          //   color: Colors.grey[200],
          // ),
          Container(
            margin:
                const EdgeInsets.only(top: 16, bottom: 14, left: 19, right: 19),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "加入黑名单",
                  style: TextStyle(
                      color: ThemeColor.textColor,
                      fontSize: 16,
                      fontWeight: FontWeight.normal),
                ),
                CupertinoSwitch(
                    value: _switchSelected,
                    activeColor: ThemeColor.appColor,
                    onChanged: (value) {
                      setState(() {
                        _switchSelected = value;
                        deleteUser(value);
                      });
                    }),
              ],
            ),
          ),
          Container(
            height: 0.5,
            color: ThemeColor.lineColor,
          ),
          GestureDetector(
            onTap: () {
              Get.to(const ComplaintListSubmitPage());
            },
            child: Container(
              color: Colors.white,
              margin: const EdgeInsets.only(
                  top: 16, bottom: 14, left: 19, right: 19),
              alignment: Alignment.centerLeft,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "投诉",
                    style: TextStyle(
                        color: ThemeColor.textColor,
                        fontSize: 16,
                        fontWeight: FontWeight.normal),
                  ),
                  const Icon(
                    Icons.arrow_forward_ios_rounded,
                    size: 20,
                    color: Color(0xFFBFBFBF),
                  ),
                ],
              ),
            ),
          ),
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, right: 19, left: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "置顶聊天",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       CupertinoSwitch(
          //           value: _switchSelected1,
          //           activeColor: ThemeColor.appColor,
          //           onChanged: (value) {
          //             setState(() {
          //               _switchSelected1 = value;
          //             });
          //           }),
          //     ],
          //   ),
          // ),
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, right: 19, left: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "提醒",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       CupertinoSwitch(
          //           value: _switchSelected2,
          //           activeColor: ThemeColor.appColor,
          //           onChanged: (value) {
          //             setState(() {
          //               _switchSelected2 = value;
          //             });
          //           }),
          //     ],
          //   ),
          // ),
          // Divider(
          //   height: 9,
          //   thickness: 9,
          //   color: Colors.grey[200],
          // ),
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, left: 19, right: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "设置当前背景",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       Icon(
          //         Icons.navigate_next,
          //         color: ThemeColor.textColorSecond,
          //         size: 20,
          //       ),
          //     ],
          //   ),
          // ),
          // Divider(
          //   height: 9,
          //   thickness: 9,
          //   color: Colors.grey[200],
          // ),
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, left: 19, right: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "清空聊天记录",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       // Icon(
          //       //   Icons.navigate_next,
          //       //   color: ThemeColor.textColor,
          //       //   size: 30,
          //       // ),
          //     ],
          //   ),
          // ),
        ],
      ),
    );
  }

  void deleteUser(bool isAdd) async {
    String userEaseName = _userInfo['easeuserId'];
    print('环信id' + userEaseName);
    if (userEaseName.isNotEmpty) {
      if (isAdd) {
        ConfirmAlertDialog.showView(context, content: '加入黑名单以后收不到消息',
            leftAction: (){
             setState(() {
               _switchSelected=false;
             });
            },
            rightAction: () async{
          try {
            await EMClient.getInstance.contactManager
                .addUserToBlockList(userEaseName);
          } on EMError catch (e) {
            debugPrint("error code: ${e.code}, desc: ${e.description}");
          }
        });
      } else {
        try {
          await EMClient.getInstance.contactManager
              .removeUserFromBlockList(userEaseName);
        } on EMError catch (e) {
          debugPrint("error code: ${e.code}, desc: ${e.description}");
        }
      }
    }
  }

  Widget _buildHead() {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.fromLTRB(16, 19, 0, 19),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Column(
            children: [
              _userInfo['profilePhoto'] != null
                  ? ClipRRect(
                      borderRadius: BorderRadius.circular(5),
                      child: Image.network(
                        _userInfo['profilePhoto'],
                        width: 50,
                        height: 50,
                        fit: BoxFit.cover,
                      ),
                    )
                  : Container(),
              const SizedBox(
                height: 3,
              ),
              Text(
                _userInfo['nickName'] ?? "",
                style: const TextStyle(
                  color: Color(0xff727272),
                  fontSize: 12,
                ),
              )
            ],
          ),
          const SizedBox(
            width: 12,
          ),
          InkWell(
            child: SizedBox(
              width: 60,
              child: Image.asset(
                'images/+.png',
                height: 50,
                width: 50,
              ),
            ),
            onTap: () {
              if (_userInfo['easeuserId'].toString().isEmpty) return;
              Get.to(
                  AddGroupMember(_userInfo['easeuserId'].toString(),
                      _userInfo['id'].toString(), '1'),
                  routeName: PageRouteNames.addGroupMemberPage);
            },
          )
        ],
      ),
    );
  }

}
